package com.lake.banner.transformer;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

/**
 * 淡入淡出效果
 */
public class FadePageTransformer implements ViewPager.PageTransformer {
    @Override
    public void transformPage(@NonNull View view, float position) {
        int pageWidth = view.getWidth();    //得到view宽

        if (position < -1) { // [-Infinity,-1)
            view.setAlpha(0);

        } else if (position <= 1) { // [-1,1]
            if (position < 0) {
                view.setTranslationX(-pageWidth * position);  //阻止消失页面的滑动
            } else {
                view.setTranslationX(pageWidth);        //直接设置出现的页面到底
                view.setTranslationX(-pageWidth * position);  //阻止出现页面的滑动
            }
            float alphaFactor = Math.max(0.0f, 1 - Math.abs(position));
            view.setAlpha(alphaFactor);
        } else { // (1,+Infinity]
            view.setAlpha(0);
        }
    }
}
